package com.lgmshare.k3.manager;

import com.lgmshare.k3.model.UserInfo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class AppDataSyncManager {

    private static volatile AppDataSyncManager mInstance;

    private AppDataSyncManager() {
    }

    public static AppDataSyncManager getInstance() {
        if (mInstance == null) {
            synchronized (AppDataSyncManager.class) {
                if (mInstance == null) {
                    mInstance = new AppDataSyncManager();
                }
            }
        }
        return mInstance;
    }

    //用户信息更新监听
    private List<OnUserInfoUpdateListener> onUserInfoUpdateListeners = new ArrayList<>();

    public void sendUserLoginStatusChangeBroadcast(boolean login) {
        Iterator<OnUserInfoUpdateListener> iterator = onUserInfoUpdateListeners.iterator();
        OnUserInfoUpdateListener listener;
        while (iterator.hasNext()) {
            listener = iterator.next();
            listener.onUserLoginStatusChange(login);
        }
    }

    public void sendUserInfoUpdateBroadcast(UserInfo userInfo) {
        Iterator<OnUserInfoUpdateListener> iterator = onUserInfoUpdateListeners.iterator();
        OnUserInfoUpdateListener listener;
        while (iterator.hasNext()) {
            listener = iterator.next();
            listener.onUserInfoChange(userInfo);
        }
    }

    public void addUserInfoUpdateListener(OnUserInfoUpdateListener listener) {
        if (listener == null) {
            return;
        }
        if (!onUserInfoUpdateListeners.contains(listener)) {
            onUserInfoUpdateListeners.add(listener);
        }
    }

    public void removeUserInfoUpdateListener(OnUserInfoUpdateListener listener) {
        if (listener == null) {
            return;
        }
        onUserInfoUpdateListeners.remove(listener);
    }
}
